package io.github.jsp.dao;

import io.github.jsp.model.JobInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface JobInfoMapper {
    
    @Select("SELECT * FROM sys_job_info WHERE id = #{id}")
    JobInfo selectById(Integer id);
    
    @Select("SELECT * FROM sys_job_info ORDER BY id")
    List<JobInfo> selectAll();
    
    @Select("SELECT * FROM sys_job_info WHERE job_status = #{status} ORDER BY id")
    List<JobInfo> selectByStatus(Integer status);
    
    @Select("SELECT * FROM sys_job_info WHERE trigger_status = #{triggerStatus} ORDER BY id")
    List<JobInfo> selectByTriggerStatus(Integer triggerStatus);
    
    @Select("SELECT * FROM sys_job_info WHERE job_group = #{jobGroup} ORDER BY id")
    List<JobInfo> selectByGroup(String jobGroup);
    
    @Insert("INSERT INTO sys_job_info (job_group, job_desc, job_cron, job_class_name, job_param, " +
            "job_status, author, alarm_email, executor_route_strategy, executor_fail_retry_count, " +
            "executor_timeout, child_jobid, trigger_status, trigger_last_time, trigger_next_time) " +
            "VALUES (#{jobGroup}, #{jobDesc}, #{jobCron}, #{jobClassName}, #{jobParam}, " +
            "#{jobStatus}, #{author}, #{alarmEmail}, #{executorRouteStrategy}, #{executorFailRetryCount}, " +
            "#{executorTimeout}, #{childJobid}, #{triggerStatus}, #{triggerLastTime}, #{triggerNextTime})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(JobInfo jobInfo);
    
    @Update("UPDATE sys_job_info SET job_group=#{jobGroup}, job_desc=#{jobDesc}, job_cron=#{jobCron}, " +
            "job_class_name=#{jobClassName}, job_param=#{jobParam}, job_status=#{jobStatus}, " +
            "author=#{author}, alarm_email=#{alarmEmail}, executor_route_strategy=#{executorRouteStrategy}, " +
            "executor_fail_retry_count=#{executorFailRetryCount}, executor_timeout=#{executorTimeout}, " +
            "child_jobid=#{childJobid}, trigger_status=#{triggerStatus}, trigger_last_time=#{triggerLastTime}, " +
            "trigger_next_time=#{triggerNextTime}, update_time=CURRENT_TIMESTAMP WHERE id=#{id}")
    int update(JobInfo jobInfo);
    
    @Update("UPDATE sys_job_info SET trigger_last_time=#{lastTime}, trigger_next_time=#{nextTime}, " +
            "update_time=CURRENT_TIMESTAMP WHERE id=#{id}")
    int updateTriggerTime(@Param("id") Integer id, @Param("lastTime") Long lastTime, @Param("nextTime") Long nextTime);
    
    @Update("UPDATE sys_job_info SET trigger_status=#{triggerStatus}, update_time=CURRENT_TIMESTAMP WHERE id=#{id}")
    int updateTriggerStatus(@Param("id") Integer id, @Param("triggerStatus") Integer triggerStatus);
    
    @Delete("DELETE FROM sys_job_info WHERE id = #{id}")
    int deleteById(Integer id);
}